/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-16 下午6:42
 */

package com.lenovo.mso2o.ui.adapter;

import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.chat.EMChatManager;
import com.lenovo.framework.base.BaseListAdapter;
import com.lenovo.framework.biz.ContextManager;
import com.lenovo.framework.util.ViewHolder;
import com.lenovo.mso2o.R;
import com.lenovo.mso2o.service.UploadService;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by lit on 2015/12/3.
 */

public class NavigationViewAdapter extends BaseListAdapter<Pair<Integer, Integer>> {


    private int sum;//会话数量
    public static final int[] icons = new int[]{
            //R.drawable.nav_view_im,
            R.drawable.nav_view_set,

            R.drawable.synchronize,
            //R.drawable.about,
            //R.drawable.nav_view_modify,

            R.drawable.nav_view_modify,
            R.drawable.about,
            R.drawable.nav_view_exit,

    };
    public static final int[] titles = new int[]{
            //R.string.nav_view_im,
            R.string.nav_view_set,

            R.string.nav_view_synchronize,
            //R.string.nav_view_modify,
            R.string.nav_view_modify,
            R.string.nav_view_about,
            R.string.nav_view_exit,
    };

    /**
     * list == null, not save adater data state, list!=null, save it's state
     * util next shown
     *
     * @param list
     */
    public NavigationViewAdapter(List<Pair<Integer, Integer>> list) {
        super(list);
        for (int i = 0; i < icons.length; i++)
            getList().add(new Pair<Integer, Integer>(icons[i], titles[i]));
    }

    @Override
    protected View getNextView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.activity_main_drawer_item, null);
        }
        ImageView image = ViewHolder.get(convertView, R.id.nav_view_drawer_image);
        TextView alart = ViewHolder.get(convertView, R.id.nav_view_drawer_alart);
        TextView title = ViewHolder.get(convertView, R.id.nav_view_drawer_text);
        ImageView line = ViewHolder.get(convertView, R.id.nav_view_line);
        image.setImageResource(icons[position]);
        title.setText(ContextManager.context().getString(titles[position]));
        //default style
        line.setVisibility(View.VISIBLE);
        alart.setVisibility(View.GONE);

        if (position == 0) {
            alart.setVisibility(View.VISIBLE);
            alart.setBackgroundResource(R.drawable.nav_view_alert_blue_shape);
            //TODO
            sum = EMChatManager.getInstance().getUnreadMsgsCount();
            if (sum == 0) {
                alart.setVisibility(View.GONE);
            } else {
                alart.setVisibility(View.VISIBLE);
                alart.setText(String.valueOf(sum));
            }

        }else if(position == 1){//服务器信息同步
            DecimalFormat df   = new DecimalFormat("######0.00");
            alart.setVisibility(View.VISIBLE);
            alart.setText(String.valueOf(df.format(UploadService.getUploadingMB())) + "M");
        }
        if (position == getCount() - 1) {
            line.setVisibility(View.GONE);
        }
        return convertView;
    }

    @Override
    protected void onReachBottom() {

    }

    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    public void setSum(int sum) {
        this.sum = sum;
    }
}
